Skip to content

[Engine] Reuse Inkling prompt checkpoints with unchanged images - #1

Open
kurtmj93 wants to merge 4 commits into
jbox5:fix/ink6627-hip-ckptfrom
kurtmj93:inkling-vision-prompt-cache
Open

kurtmj93 wants to merge 4 commits into
jbox5:fix/ink6627-hip-ckptfrom
kurtmj93:inkling-vision-prompt-cache

Conversation

@kurtmj93

Copy link
Copy Markdown

Problem and result

Image-bearing requests skip prompt checkpoints in the current draft, including text follow-ups that resend historical screenshots. This change lets Inkling resume when the images in the retained prefix have the same bytes and expanded token positions. A new image in the suffix is still encoded. Replacing an image forces a reread when it invalidates the checkpoint.

Related discussion

Follow-up for Niko1221#6665 and Niko1221#6627. This PR targets the branch behind Niko1221#6665.

Approach

Hash the image bytes with SHA-256 rather than comparing the per-request filenames or image placeholder tokens. Validate image positions and dimensions before resuming, and skip the vision encoder for images covered by the reused prefix. Use the engine's expanded prompt length when reporting multimodal usage.

Validation

Passed:

  • On this PR revision, configured CMake with HIP enabled and architecture gfx1100, then ran cmake --build pr6665-build --target strata-inkling -j 8 on Windows. The build passed with two existing warnings.
  • On this PR revision, python -m unittest serve.test_request_accounting serve.test_yield_accounting passed all 18 tests.
  • A local OpenAI-compatible API replay on Windows 11 and RX 7900 XTX (gfx1100, 24 GB) used this change together with the separate [Engine] HIP - fix NaN by using per-expert codecs for mixed prefill layers ("the model's output became non-finite (NaN) twice in a row") Niko1221/Strata#6594 NaN fix. Inkling 2 Mini SQ-15, FP8 KV, drafting off, chunk 256, 262,144-token context, one slot. The 17-image cold request read 8,647 tokens in 5.84 s. The streaming text follow-up reused 8,647 of 8,668 tokens and spent 0.20 s in prefill. Appending one green image reused 8,668 tokens and answered green. Replacing the first image with a green image of the same size reported zero reuse and answered green.

Failed: None in the final runs.

Not run: GPU inference on this exact revision without Niko1221#6594, and CUDA, Metal or SYCL builds. Kept as a draft for that coverage.

Limits

This uses the parent PR's per-slot checkpoint. Its slot limits and fallback behavior still apply. An edit before the saved checkpoint can still require a full read.

martinshane and others added 4 commits November 22, 2026 20:21
Multi-turn chats never reuse the slot's KV in strata-inkling: the prefix match
in ink_serve.h is all-or-nothing against cached_prefix, and cached_prefix holds
the previous reply, including the hidden reasoning span that clients never send
back. So every turn resets the slot and reads the whole conversation again.

This adds three backend hooks with no-op defaults (checkpoint_slot,
restore_checkpoint, drop_checkpoint) to InkBackend. The serve loop takes a
checkpoint when a slot has read a whole prompt (no images), before any
generated token enters the slot. On admission, when the new prompt diverges
from cached_prefix inside the previous reply but still contains the whole
previous prompt, it restores the checkpoint and resumes at ckpt_len instead of
resetting to 0, and logs "INFO prompt-checkpoint resume N". Reset paths drop
the checkpoint.

Backends that do not override the hooks keep the old behaviour.

Based on the diff by martinshane in Niko1221#6627 (ink_backend.h and ink_serve.h hunks,
unchanged; the Metal backend part of that diff is not included here).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SG5G8VwxLyU5bwfggEz6AU
HIP side of checkpoint_slot / restore_checkpoint / drop_checkpoint, so the
Niko1221#6627 serve-side resume also works on AMD GPUs.

- SWA visits keep a ring of swa_win positions in 64-token pages (ensure_page:
  logical page (p % swa_win) >> 6). checkpoint_slot copies the slot's mapped
  ring pages of every SWA visit to pinned host memory, one hipMemcpyAsync per
  run of consecutive physical pages, K and V (and the per-token scales with
  FP8 KV). About 160 MiB of host RAM per slot at BF16 KV, no VRAM.
- ShortConv state (n_visits x 3 lags) is copied to small device buffers with
  k_sconv_snapshot_pair and restored with k_sconv_rollback_pair at
  keep_steps = 0 (all three lags come from the snapshot argument).
- Global-visit KV and the DSpark context / f_rnn are indexed by absolute
  position and only appended, so restore sets seq_len back (forward_batch
  only raises it with std::max) and caps dspark_len.
- restore_checkpoint first checks that the slot still maps the same physical
  SWA pages (only free_slot unmaps them; grow_pool keeps page indices) and
  otherwise returns false, so the serve falls back to a full read. It resets
  DSpark's adaptive draft state as reset_slot does.
- Every copy is queued on the backend stream, in order with the forward
  passes; no host sync is added.
- Off when max_slots > 4, to bound pinned host RAM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SG5G8VwxLyU5bwfggEz6AU
The serve reads fields 6-8 of DONE (drafts accepted, drafts offered,
reused) into usage.prompt_tokens_details.cached_tokens, timings.cache_n,
timings.draft_n / draft_n_accepted and the /metrics draft counters
(serve/server.py StrataEngine._parse_done, server_parts/core.py
request_timings). strata-inkling printed only 5 fields, so a prompt
resumed from the slot (RESUME, or the Niko1221#6627 checkpoint) still showed
cached_tokens 0, and DSpark acceptance was never visible (Niko1221#6633).

Single slot: DONE now carries the request's draft counters and reuse.
--batch admission: DONE carries 0 0 <reused>; the slot's drafts still
arrive on BDONE (drafts=), which the serve sums onto them. The counters
already existed in ActiveReq; rq.reused is set where RESUME is printed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SG5G8VwxLyU5bwfggEz6AU
Match SHA-256 image content and expanded token positions before resuming. Encode only the images in the fresh suffix and count expanded prompt tokens in the API and metrics.
@kurtmj93
kurtmj93 marked this pull request as ready for review November 23, 2026 00:26
@jbox5
jbox5 force-pushed the fix/ink6627-hip-ckpt branch from a6babf9 to fe2ab3b Compare November 23, 2026 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants